/**
 *  *2010-11-8 下午02:04:26  add 
 */
package com.base.myproject.client.busi.gcode;

import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.tools.CommandSyncsql;
import com.base.myproject.client.tools.GreetingService;
import com.base.myproject.client.tools.GreetingServiceAsync;
import com.base.myproject.client.ui.celleditor.BaseComBoxFromBinding;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.base.myproject.client.ui.celleditor.BaseComboxXBinding;
import com.extjs.gxt.ui.client.binding.FieldBinding;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * @author Administrator
 *2010-11-8
 * javazgw@gmail.com
 */
public class GcodeAddDialog extends Dialog	implements Listener<BaseEvent>,AsyncCallback<CommandSyncReturnObject>{
	
	BusiObject bo ;
	FormPanel formpanel = new FormPanel();
	/**
	 * cgicode,cgcode,gcode,modeltype,buyprice,saleprice
	 * @param bo 
	 */
	TextField<String> cgicode = new TextField<String>();
	TextField<String> cgcode = new TextField<String>();
	TextField<String> gcode = new TextField<String>();
	TextField<String> modeltype = new TextField<String>();
	TextField<String> buyprice = new TextField<String>();
	TextField<String> saleprice = new TextField<String>();
	TextField<String> saleprice2 = new TextField<String>();
	TextField<String> saleprice3 = new TextField<String>();
	FormBinding fb ;
	public GcodeAddDialog(BusiObject bo)
	{
		super();
		this.bo = bo;
		//fb= new FormBinding(formpanel,true);
		this.setTitle("添加新商品");
		setAutoWidth(true);
	}
	protected void onRender(Element parent, int pos) {
	    super.onRender(parent, pos);
	    cgicode.setFieldLabel("Barcode");
	    cgcode.setFieldLabel("名称");
	    gcode.setFieldLabel("类型");
	    modeltype.setFieldLabel("规格");
	    buyprice.setFieldLabel("采购价格"); 
	    saleprice.setFieldLabel("销售价格1");
	    saleprice2.setFieldLabel("销售价格2");
	    saleprice3.setFieldLabel("销售价格3");
	    
	    cgicode.setName("cgicode");
	    cgcode.setName("cgcode");
	    gcode.setName("gcode");
	    modeltype.setName("modeltype");
	    buyprice.setName("buyprice");
	    saleprice.setName("saleprice");
	    saleprice2.setName("saleprice2");
	    saleprice3.setName("saleprice3");
	    
	    formpanel.add(cgicode);
	    formpanel.add(cgcode);
	    formpanel.add(gcode);
	    formpanel.add(modeltype);
	    formpanel.add(buyprice);
	    formpanel.add(saleprice);
	    formpanel.add(saleprice2);
	    formpanel.add(saleprice3);
	    
	   // setSize(260, 400);
	    formpanel.setAutoHeight(true);
	    formpanel.setWidth(339);
	    formpanel.setHeaderVisible(false);
	    
	    fb = new FormBinding(formpanel, true){
			  public void autoBind() {
				    for (Field<?> f : panel.getFields()) {
				      if (!bindings.containsKey(f.getId())) {
				        String name = f.getName();
				        if (name != null && name.length() > 0) {
				        	if(f instanceof BaseComboBoxForm<?>)
				        	{
				        		
				        		BaseComBoxFromBinding bcbfb = new BaseComBoxFromBinding((BaseComboBoxForm)f,f.getName());
				        		bindings.put(f.getId(), bcbfb);
				        	}
				        	/**
				        	 * BaseComboBoxX  方式的数据绑定
				        	 */
				        	else if(f instanceof BaseComboBoxX)
				        	{
				        		BaseComboxXBinding bcbxb = new BaseComboxXBinding((BaseComboBoxX)f,f.getName());
				        		bindings.put(f.getId(), bcbxb);
				        	}
				        	else
				        	{
				        		FieldBinding b = new FieldBinding(f, f.getName());
				        		bindings.put(f.getId(), b);
				        	}
				        }
				      }
				    }
				  }

		};
	    
	    
		 FieldBinding b = new FieldBinding(cgicode, cgicode.getName());
		 fb.addFieldBinding(b);
	    fb.bind(bo);
		add(formpanel);
		setButtons(Dialog.OKCANCEL);
		getButtonById(Dialog.CANCEL).addListener(Events.Select, this);
		getButtonById(Dialog.OK).addListener(Events.Select, this);
	    
	}
	protected void add()
	{
		
		if(!formpanel.isValid())
		{
			MessageBox.alert("提示", "有内容没有录入完整", null);
			return;
		}
		getButtonById(Dialog.OK).setEnabled(false);
		GWT.log(this.bo.insert());
		CommandSyncsql csql = new CommandSyncsql();
		
		csql.getV().add(this.bo.insert());
		 CommandSyncContainer list = new CommandSyncContainer();
		 list.add(csql);
		 
		 GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);
		 greetingService
			.SendCommandSync(
					list,this
					);
	}
	@Override
	public void handleEvent(BaseEvent be) {
		  if(be.getSource() == getButtonById(Dialog.CANCEL))
			{
				//d.setVisible(false);
			  setVisible(false);
			}
			else if(be.getSource() == getButtonById(Dialog.OK))
			{
				add();
			}
		
	}
	@Override
	public void onFailure(Throwable caught) {
		MessageBox.alert("错误",
				"网络连接失败", null);
		getButtonById(Dialog.OK).setEnabled(true);
	}
	@Override
	public void onSuccess(CommandSyncReturnObject result) {
		if (result.isB()) {
			Info.display("", "添加成功");
			addsuccess();
		}
		else{
			MessageBox.alert("错误",
					"错误，错误代码"+result.getMessage(), null);
			this.getButtonById(Dialog.OK).setEnabled(true);
			
		}
	}

	public void addsuccess()
	{
		this.hide();
	}
}
